package logic

import (
	"context"
	"jsa/goods-rpc/internal/model"

	"jsa/goods-rpc/goods"
	"jsa/goods-rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetGoodsByOfLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetGoodsByOfLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGoodsByOfLogic {
	return &GetGoodsByOfLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetGoodsByOfLogic) GetGoodsByOf(in *goods.GetsGoodsRequest) (*goods.GetsGoodsResponse, error) {
	g := model.NewGoods()
	getsGoods, i, err := g.GetsGoods(in.Offset, in.Limit)
	if err != nil {
		return nil, err
	}
	var good []*goods.GoodsIn
	for _, v := range getsGoods {
		o := &goods.GoodsIn{
			GoodsId: int64(v.ID),
			Title:   v.Title,
			Img:     v.Img,
			Price:   v.Price,
		}
		good = append(good, o)
	}
	return &goods.GetsGoodsResponse{
		GoodsInfo: good,
		Total:     i,
	}, nil
}
